package com.mzkj.wanjulaile.mobile.api.modules.account.controller;

import com.mzkj.common.data.StatusCode;
import com.mzkj.common.utils.DateUtils;
import com.mzkj.common.utils.StringUtils;
import com.mzkj.wanjulaile.mobile.api.common.controller.BaseController;
import com.mzkj.wanjulaile.mobile.api.common.entity.ResultEntity;
import com.mzkj.wanjulaile.mobile.api.modules.account.constant.Constant;
import com.mzkj.wanjulaile.mobile.api.modules.account.entity.Account;
import com.mzkj.wanjulaile.mobile.api.modules.account.entity.BuyVip;
import com.mzkj.wanjulaile.mobile.api.modules.account.entity.VipCategory;
import com.mzkj.wanjulaile.mobile.api.modules.account.entity.VipScheme;
import com.mzkj.wanjulaile.mobile.api.modules.account.service.AccountService;
import com.mzkj.wanjulaile.mobile.api.modules.account.service.BuyVipService;
import com.mzkj.wanjulaile.mobile.api.modules.account.service.VipCategoryService;
import com.mzkj.wanjulaile.mobile.api.modules.account.service.VipSchemeService;
import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping(value = "auth/vipCategory")
public class VipCategoryAuthController extends BaseController {
    @Autowired
    private VipCategoryService vipCategoryService;
    @Autowired
    private VipSchemeService vipSchemeService;
    @Autowired
    private AccountService accountService;
    @Autowired
    private BuyVipService buyVipService;

    /**
     * 获取VIP类型列表
     * @param request  accessToken
     * @return
     */
    @RequestMapping(value = "becomeVip")
    @ResponseBody
    public ResultEntity<List<VipCategory>> getVipCategoryList(HttpServletRequest request){
        ResultEntity<List<VipCategory>> result = new ResultEntity<List<VipCategory>>();
        try {
            String accountId=(String)request.getAttribute("accountId");
            Account account = accountService.get2(accountId);
            VipCategory vipCategory = new VipCategory();
            vipCategory.setShowFlag("0");
            List<VipCategory> vipCategoryList = vipCategoryService.wxFindList(vipCategory,account.getId());
            result.setStatusCode(StatusCode.SC0.getValue());
            result.setMsg(StatusCode.SC0.getDesc());
            result.setData(vipCategoryList);
        } catch (Exception e) {
            e.printStackTrace();
            result.setStatusCode(StatusCode.SC5000.getValue());
            result.setMsg(StatusCode.SC5000.getDesc());
        }
        return result;
    }

    /**
     * 根据vip类别获取详情和vip制度列表
     * @param id  vip类型id
     * @return
     */
    @RequestMapping(value = "getVipCategory")
    @ResponseBody
    public ResultEntity<VipCategory> getVipCategory(String id){
        ResultEntity<VipCategory> result=new ResultEntity<VipCategory>();
        if(StringUtils.isEmpty(id)){
            return new ResultEntity<>(StatusCode.SC4001);
        }
        try{
            VipCategory vipCategory=vipCategoryService.getVipSchemeByCategory(new VipCategory(id));
            vipCategory.setRule(StringEscapeUtils.unescapeHtml4(vipCategory.getRule()));
            result.setStatusCode(StatusCode.SC0.getValue());
            result.setMsg(StatusCode.SC0.getDesc());
            result.setData(vipCategory);
        }catch (Exception e){
            logger.error("获取VIP类型数据异常",e);
            result.setStatusCode(StatusCode.SC5000.getValue());
            result.setMsg(StatusCode.SC5000.getDesc());
        }
        return result;
    }

    /**
     * 校验会员购买是否符合规则
     * @param vipSchemeId  vip制度id
     * @param request      accessToken
     * @return
     */
    @RequestMapping(value = "toBuyVip")
    @ResponseBody
    public ResultEntity<String> toBuyVip(String vipSchemeId, HttpServletRequest request){
        if(StringUtils.isEmpty(vipSchemeId)){
            return new ResultEntity<>(StatusCode.SC4001);
        }
        ResultEntity<String> result = new ResultEntity<>();
        try{
            String accountId=(String)request.getAttribute("accountId");
            VipScheme vipScheme = vipSchemeService.get(vipSchemeId);
            VipCategory vipCategory = vipScheme.getVipCategory();
            Account account = accountService.get2(accountId);
            if (account.getVipType().equals(Constant.WAN_MEMBER) || account.getVipType().equals(Constant.TEMP_STAR_VIP)){
                result.setStatusCode(StatusCode.SC0.getValue());
                result.setMsg(StatusCode.SC0.getDesc());
            }else if (account.getVipType()<vipCategory.getType()){
                //会员升级
                VipScheme lastVipScheme = vipSchemeService.get(account.getVipScheme().getId());
                BuyVip curBuyVip = buyVipService.getSimple(account.getCurBuyVip());
                Date date = buyVipService.setAccountUpVipSpan(account, vipScheme,lastVipScheme,curBuyVip.getFee());

                Long twoDate = DateUtils.getDistanceOfTwoDate(new Date(), date);
                String leftDays = getUpVipLeftDays(twoDate);
                result.setStatusCode(StatusCode.SC4009.getValue());
                result.setMsg("您目前是" + account.getVipTypeText() + "，选择升级后，您剩余套餐时长将按新套餐进行折算，合计:"+leftDays);
            }else if (account.getVipType()>vipCategory.getType()){
                //会员降级
                result.setStatusCode(StatusCode.SC4008.getValue());
                result.setMsg("您目前是" + account.getVipTypeText() + "，无法直接购买" + vipCategory.getName() + "套餐，可先取消当前会员再进行购买");
            }else {
                result.setStatusCode(StatusCode.SC0.getValue());
                result.setMsg(StatusCode.SC0.getDesc());
            }
        }catch (Exception e){
            e.printStackTrace();
            result.setStatusCode(StatusCode.SC5000.getValue());
            result.setMsg(StatusCode.SC5000.getDesc());
        }
        return result;

    }

    private String getUpVipLeftDays(Long twoDate) {
        String leftDays = "";
        if (twoDate != null){
            if (twoDate/30 == 0){
                leftDays = twoDate/30+"个月";
            }else{
                leftDays = twoDate/30+"个月"+twoDate%30+"天";
            }
        }
        return leftDays;
    }

    @RequestMapping(value = "getVipCategoryByType")
    @ResponseBody
    public ResultEntity<VipCategory> getVipCategoryByType(Integer type){
        ResultEntity<VipCategory> result=new ResultEntity<>();
        try{
            VipCategory vipCategory=vipCategoryService.getByType(type);
            result.setStatusCode(StatusCode.SC0.getValue());
            result.setMsg(StatusCode.SC0.getDesc());
            result.setData(vipCategory);
        }catch (Exception e){
            logger.error("获取VIP类型数据异常",e);
            result.setStatusCode(StatusCode.SC5000.getValue());
            result.setMsg(StatusCode.SC5000.getDesc());
        }
        return result;
    }

}